﻿using EPE.BLL;
using EPE.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EPE.WPF
{
    /// <summary>
    /// ShopsPriceSystemAddForm.xaml 的交互逻辑
    /// </summary>
    public partial class ShopsPriceSystemAddForm : Window
    {
        public ShopsPriceSystemAddForm()
        {
            InitializeComponent();
        }

        CLoginUser userinfo = new CLoginUser();
        private ObservableCollection<CShopsPriceParaTree> ShopsPriceParaTree;

        public ShopsPriceSystemAddForm(CLoginUser pargUserinfo)
        {
            InitializeComponent();
            userinfo = pargUserinfo;

            PopupBasePara.Placement = System.Windows.Controls.Primitives.PlacementMode.RelativePoint;
            
            PopupBasePara.VerticalOffset = this.txtBasePara.Height;

            PopupBasePara.StaysOpen = true;
            PopupBasePara.Height = this.tvBasePara.Height;
            PopupBasePara.Width = txtBasePara.Width;

            ShopsPricePara bll = new ShopsPricePara();

            DataTable dt = bll.GetAllListOnTable(string.Empty, userinfo.CompanyId.ToString());

            //先清理一下节点 
            this.tvBasePara.Items.Clear();
            if (dt != null || dt.Rows.Count > 0)            //判断一线有没有数据  
            {
                //首先遍历父节点  
                TreeViewItem item;//wpf不再是节点了  改成item 其实是一样的  
                DataRow[] dr = dt.Select("parentid='0'");  //判断一下啊 ，我用ParentID（父节点的值）这个是我定义好的要根据  
                // 这个读父节点的   
                if (dr.Length != 0)
                {
                    foreach (DataRow drTemp in dr)
                    {
                        item = new TreeViewItem();
                        item.Tag = drTemp["paraid"].ToString();     //一般绑定ID  
                        item.Header = drTemp["paraname"].ToString();    //名称    
                        tvBasePara.Items.Add(item);
                        item.IsExpanded = true;
                        BindNode(item,dt);
                    }
                }
            }  

            //this.tvBasePara.DataContext = ShopsPriceParaTree;
            
        }

        /// <summary>  
        /// 遍历所有的子节点  
        /// </summary>  
        /// <param name="item">父节点的item</param>  
        private void BindNode(TreeViewItem item,DataTable dt)
        {
            DataRow[] dr = dt.Select("parentid = '" + item.Tag + "'"); //查询子节点条件  
            if (dr.Length > 0)
                foreach (DataRow drTemp in dr)
                {
                    TreeViewItem childItem = new TreeViewItem();
                    childItem.Header = drTemp["paraname"].ToString();
                    childItem.Tag = drTemp["paraid"].ToString();
                   
                    item.Items.Add(childItem);
                    item.IsExpanded = true;
                    BindNode(childItem,dt);
                }
        }  

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        private void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            ShopsProjectConditionForm spc = new ShopsProjectConditionForm(userinfo);


            spc.ShowDialog();

            if (spc.Model != null)
            {
                this.txtProject.Text = spc.Model.projectname.ToString();
                this.lblProjectId.Content = spc.Model.projectid.ToString();
            }

           
        }


        private void txtBasePara_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
        }

        private void tvBasePara_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var trv = sender as TreeView;
            var trvItem = trv.SelectedItem as TreeViewItem;
            if (trvItem.Items.Count != 0) return;   //只选叶子
            this.txtBasePara.Text = trvItem.Header.ToString();
            PopupBasePara.IsOpen = false;
        }
        private void tvBasePara_Initialized(object sender, EventArgs e)
        {
            var trv = sender as TreeView;
            var trvItem = new TreeViewItem() { Header = "初始化内容" };
            var trvItemSel = trv.Items[1] as TreeViewItem;
            trvItemSel.Items.Add(trvItem);
        }
        private void btnSel_Click(object sender, RoutedEventArgs e)
        {
            this.PopupBasePara.IsOpen = !PopupBasePara.IsOpen;
        }


    }
}
